#include "FontManager.h"

namespace Lame3d
{
	const char* _fontExtensions[] = {"ttf", NULL};
	
	FontManager::FontManager(const DisplayManager &dm, const char* path)
			: directory(path, _fontExtensions), displayManager(dm)
	{
	}
	
	FontManager::~FontManager()
	{
		std::map<FontDesc, Font*>::iterator it;
		for(it = fonts.begin(); it != fonts.end(); ++it) {
			delete it->second;
		}
	}
	
	Font& FontManager::Request(const std::string &fName, const Color &color, int size)
	{
		File& file = directory[fName];
		FontDesc desc;
		desc.file = &file;
		desc.color = color;
		desc.size = size;
		std::map<FontDesc, Font*>::iterator it;
		Font *nF;
		it = fonts.find(desc);
		if(it == fonts.end()) {
			// not found
			nF = new Font(file, displayManager, color, size);
			fonts[desc] = nF;
		} else {
			nF = it->second;
		}
		return *nF;
	}
	
	// annoying minutia
	bool FontManager::FontDesc::operator<(const FontManager::FontDesc &c) const
	{
		if(file == c.file) {
			if(size == c.size) {
				return color < c.color;
			} else {
				return size < c.size;
			}
		} else {
			return file < c.file;
		}
	}
}
